home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Skunkware 98
/
Skunkware 98.iso
/
src
/
net
/
bind-contrib.tar.gz
/
bind-contrib.tar
/
contrib
/
tic
/
readinfo
< prev
next >
Wrap
Text File
|
1996-10-25
|
4KB
|
178 lines
#!/bin/sh
# @(#) readinfo 1.3 92/08/31 @(#)
# reads a host database file
#
# input from stdin
# output to stdout
#
# arguments are names of fields in the file
#
# fields in a file are defined by a comment line of the form
# #FIELDS <field_description> ...
#
# where <field_description> is the name of the field followed by optional
# keyword parameters of the form parameter=<value>
# three paramaters are supported in this script
# prefix - prefix added to value of the field
# suffix - suffix added to value of field
# no - character to prevent adding a prefix or suffix to a field
#
# fields in the file are separated with white space. Embedded blanks in fields
# are either escaped with a '\' or the entire field is quoted with double quotes
# The prefix or suffix can be overridden by prepending or following the value
# with the ``no='' character
# Copyright (c) 1992 by Texas Internet Consulting
# This code may be freely copied and used so long as this
# copyright notice is attached. This code may not be sold
# without the express written permission of Texas Internet Consulting.
# Texas Internet Consulting makes no warranty as to the correctness
# nor the applicability of this code for any purpose.
# get the fields from the command line - will be funnelled to awk
fields="$*"
# change whitespace to tabs and handle quoted fields and escaped blanks
# also remove comment lines, except the #FIELDS line
awk '
BEGIN {
# get around double quote bug
dq = sprintf("%c", 34)
}
/^#FIELDS/ || ( ! /^#/ && ! /^$/ && ! /^[ ][ ]*/ ) {
# line has double quotes or backslashes
if ($0 ~ /"/ || $0 ~ /\\/) {
# print everything up to field with double quote or backslash
l = length($0)
n = 0
for (i=1; i<=NF; i++) {
if ($i !~ /"/ && $i !~ /\\/) {
printf("%s\t", $i)
}
n += length($i)
n++
}
remainder = substr($0, n+1, l-n)
l -= n
quoted = 0
escaped = 0
for (i=1; i<=l; i++) {
sub = substr(remainder, i, 1)
if (sub == dq) {
if (quoted) {
quoted = 0
}
else {
quoted = 1
}
}
else if (sub == "\\") {
escaped = 1
}
else if (quoted) {
printf("%s", sub)
}
else if (escaped) {
printf("%s", sub)
escaped = 0
}
else if (sub == " ") {
printf("\t")
}
else {
printf("%s", sub)
}
}
}
# line is just a bunch of normal fields
else {
for (i=1; i<=NF; i++) {
printf("%s", $i)
if (i < NF) {
printf("\t")
}
}
}
printf("\n")
}' |
# read file and output fields wanted
awk -F' ' '
# get the fields from the command line
BEGIN {
EXTRACT="'"$fields"'"
nextract = split(EXTRACT, extract, " ")
}
# extract field definition info from file
/^#FIELDS/ {
nfields = 0
for (i=2; i<=NF; i++) {
# field is a paramter
if ($i ~ /\=/) {
n = split($i, part, "=")
keyword = part[1]
value = part[2]
# prefix
if (keyword == "prefix") {
prefix[nfields] = value
}
# suffix
else if (keyword == "suffix") {
suffix[nfields] = value
}
else if (keyword == "no") {
no[nfields] = value
}
}
#field name
else {
nfields++
fields[nfields] = $i
prefix[nfields] = ""
suffix[nfields] = ""
no[nfields] = ""
}
}
}
# process a record
! /^#/ && ! /^$/ && ! /^[ ][ ]*/ {
# scan the list of fields to extract and compare them with the
# list in the file and print as they are encountered
for (i=1; i<=nextract; i++) {
for (j=1; j<=nfields; j++) {
if (fields[j] == extract[i]) {
break
}
}
if (j > nfields) {
printf("***ERROR*** field not found - %s\n", extract[i])
continue
}
if (i > 1) printf(" ")
# no overide character for this field
if (no[j] == "") {
printf("%s%s%s", prefix[j], $j, suffix[j])
continue
}
# overide character exist - split up the field
n = length($j)
first = substr($j, 1, 1)
last = substr($j, n, 1)
# no overide character in field value
if (first != no[j] && last != no[j]) {
printf("%s%s%s", prefix[j], $j, suffix[j])
continue
}
middle = ""
if (n >= 3) {
middle = substr($j, 2, n-2)
}
if (first != no[j]) {
printf("%s%s", prefix[j], first)
}
printf("%s", middle)
if (last != no[j]) {
printf("%s%s", last, suffix[j])
}
}
printf("\n")
}'